package com.lczyfz.istep.modules.openstack.api;

import com.lczyfz.istep.common.constants.SceneConstants;
import com.lczyfz.istep.modules.openstack.api.glance.impl.ImagesImpl;
import com.lczyfz.istep.modules.openstack.api.glance.interfaces.Images;
import com.lczyfz.istep.modules.openstack.api.neutron.impl.NetworksImpl;
import com.lczyfz.istep.modules.openstack.api.neutron.impl.RouterImpl;
import com.lczyfz.istep.modules.openstack.api.neutron.impl.SubnetImpl;
import com.lczyfz.istep.modules.openstack.api.neutron.impl.SubnetPoolImpl;
import com.lczyfz.istep.modules.openstack.api.neutron.impl.securitygroup.SecuritygroupImpl;
import com.lczyfz.istep.modules.openstack.api.neutron.impl.securitygroup.SecuritygroupRuleImpl;
import com.lczyfz.istep.modules.openstack.api.neutron.model.Network;
import com.lczyfz.istep.modules.openstack.api.neutron.model.Router;
import com.lczyfz.istep.modules.openstack.api.neutron.model.Subnet;
import com.lczyfz.istep.modules.openstack.api.neutron.model.SubnetPool;
import com.lczyfz.istep.modules.openstack.api.neutron.model.securitygroup.Securitygroup;
import com.lczyfz.istep.modules.openstack.api.neutron.model.securitygroup.SecuritygroupRule;
import com.lczyfz.istep.modules.openstack.api.nova.impl.FlavorsImpl;
import com.lczyfz.istep.modules.openstack.api.nova.impl.VirtualMachineImpl;
import com.lczyfz.istep.modules.openstack.api.nova.model.Flavor;
import com.lczyfz.istep.modules.openstack.api.nova.model.Server;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;

public class WorkFlow {
	
	public static void main(String[] args){
		
		//��������
		NetworksImpl network = new NetworksImpl();
		Network createnet = new Network();
		createnet.setName("test_network");
		Network new_network = network.createNetwork(createnet);
		
		//����������
		SubnetPoolImpl sp = new SubnetPoolImpl();
		ArrayList<String> subnetprefix = new ArrayList<String>();
		subnetprefix.add("192.168.0.0/16");
		SubnetPool createsp = new SubnetPool();
		createsp.setSpName("test_sp");
		createsp.setSpPrefixes(subnetprefix);
		createsp.setSpDefaultPrefixlen("16");
		SubnetPool new_sp = sp.createSubnetPool(createsp);
		
		//��������
		SubnetImpl subnet = new SubnetImpl();
		Subnet createsub = new Subnet();
		createsub.setsubnetName("test_subnet");
		createsub.setsubnetNetworkId(new_network.getId());
		createsub.setsubnetIpVersion("4");
		createsub.setsubnetPoolId(new_sp.getSpId());
		Subnet new_subnet = subnet.createSubnet(createsub);
		
		//����·��
		RouterImpl router = new RouterImpl();
		Router createrouter = new Router();
		createrouter.setRouterName("test_router");
		createrouter.setRouterNetworkId("48042b0f-4560-40b8-8458-3b09416a5721");
		Router new_router = router.createRouter(createrouter);
		router.addRouterInterface(new_router.getRouterId(), new_subnet.getsubnetId(), SceneConstants.routerAndNetwork);
		
		//������ȫ��
		SecuritygroupImpl sg = new SecuritygroupImpl();
		Securitygroup createsg = new Securitygroup();
		createsg.setSecuritygroupName("test_sg");
		Securitygroup new_sg = sg.createSecuritygroup(createsg);
		
		//������ȫ�����
		SecuritygroupRuleImpl sr = new SecuritygroupRuleImpl();
		SecuritygroupRule createsr = new SecuritygroupRule();
		createsr.setSrDirection("ingress");
		createsr.setSrProtocol("tcp");
		createsr.setSrSecurityGroupId(new_sg.getSecuritygroupId());
		createsr.setSrPortMin("80");
		createsr.setSrPortMax("80");
		SecuritygroupRule new_sr = sr.createSecuritygroupRule(createsr);
		
		//��������������
		FlavorsImpl flavor = new FlavorsImpl();
		Flavor createflavor = new Flavor();
		createflavor.setFlavorId("10");
		createflavor.setFlavorName("test_flavor");
		createflavor.setFlavorRam("64");
		createflavor.setFlavorVcpus("1");
		createflavor.setFlavorDisk("0");
		Flavor new_flavor = flavor.createFlavor(createflavor);
	/*	
		Images images = new ImagesImpl();
		Image image = new Image();
		image.setName("ubuntu");
		image.setDiskFormat("iso");
	
		Image new_image = images.creatImage(image);
	*/	
		//���������
		Images images = new ImagesImpl();
		
		VirtualMachineImpl nova = new VirtualMachineImpl();
		System.out.println("���������");
		Server newServer = new Server();
		newServer.setName("1234567");
		newServer.setFlavorRef(new_flavor.getFlavorId());
		newServer.setImageRef(images.getImages().get(2).getId());
		
		JSONArray networks = new JSONArray();
		JSONObject network1 = new JSONObject();
	
		network1.put("uuid", new_network.getId());
		networks.add(0, network1);
		System.out.println(networks);
		newServer.setNetworks(networks);
		Server res = nova.addServer(newServer);
		System.out.println("�����ɹ�");

	}

}
